package api

import (
	"FeedXmh/app/dao"
	jwt "github.com/gogf/gf-jwt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

var Api_map = api_map{}

type api_map struct{}

func (*api_map) GET(r *ghttp.Request) {
	where := "1=1 "
	params := r.GetQueryMap()

	id := gconv.String(params["id"])
	if id != "" {
		where += " and id='" + id + "'"
	}

	func_id := gconv.String(params["func_id"])
	if func_id != "" {
		where += " and func_id='" + func_id + "'"
	}

	action_id := gconv.String(params["action_id"])
	if action_id != "" {
		where += " and action_id='" + action_id + "'"
	}

	method := gconv.String(params["method"])
	if method != "" {
		where += " and method='" + method + "'"
	}

	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url, r.Method, userid) {
		res, err := db.Model("v_api_map").Where(where).Order("id asc").All()
		if err != nil {
			g.Log().Line().Error(err)
		}
		r.Response.WriteJson(g.Map{
			"code":  0,
			"msg":   "",
			"count": len(res),
			"data":  res.List(),
		})
	} else {
		r.Response.WriteJson(g.Map{
			"code":  -1,
			"msg":   "permission denied",
			"count": 0,
			"data":  nil,
		})
	}
	r.ExitAll()
}

func (*api_map) PUT(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
	}
	params := r.GetFormMap()
	claims := jwt.ExtractClaims(r)
	current_user := claims["userid"].(string)
	url := r.URL.Path
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url, r.Method, current_user) {
		sqlres, _ := db.Model("t_api_map").Data(params).Where("id=?", params["id"]).Update()
		if a, err := sqlres.RowsAffected(); err == nil && a >= 0 {
			res["code"] = 0
			res["msg"] = "保存成功"
		} else {
			res["code"] = -1
			res["msg"] = "保存失败"
		}
	} else {
		res["code"] = -1
		res["msg"] = "Permission denied"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}

func (*api_map) POST(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
	}
	params := r.GetFormMap()
	claims := jwt.ExtractClaims(r)
	current_user := claims["userid"].(string)
	url := r.URL.Path
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url, r.Method, current_user) {
		//判断用户是否已经存在
		count, _ := db.Model("t_api_map").Where("func_id=? and api=? and method=?", params["func_id"], params["api"], params["method"]).Count()
		if count > 0 {
			res["code"] = -1
			res["msg"] = "已经存在"
		} else {
			delete(params, "id")
			sqlres, _ := db.Model("t_api_map").Data(params).Insert()
			if a, err := sqlres.RowsAffected(); err == nil && a > 0 {
				res["code"] = 0
				res["msg"] = "保存成功"
			} else {
				res["code"] = -1
				res["msg"] = "保存失败"
			}
		}
	} else {
		res["code"] = -1
		res["msg"] = "Permission denied"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}

func (*api_map) DELETE(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
	}
	id := r.GetFormMap()["id"]
	claims := jwt.ExtractClaims(r)
	current_user := claims["userid"].(string)
	url := r.URL.Path
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url, r.Method, current_user) {
		sqlres, _ := db.Model("t_api_map").Where("id=?", id).Delete()
		if a, err := sqlres.RowsAffected(); err == nil && a >= 0 {
			res["code"] = 0
			res["msg"] = "保存成功"
		} else {
			res["code"] = -1
			res["msg"] = "保存失败"
		}
	} else {
		res["code"] = -1
		res["msg"] = "Permission denied"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}
